package com.sakiko.audiobook.account.api;

import com.sakiko.audiobook.account.service.UserAccountService;
import com.sakiko.audiobook.common.login.SakiLogin;
import com.sakiko.audiobook.common.result.Result;
import com.sakiko.audiobook.common.util.AuthContextHolder;
import com.sakiko.audiobook.model.account.AccountDeductVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"all"})
public class UserAccountApiController {

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 获取账户余额
     *
     * @return
     */
    @SakiLogin
    @Operation(summary = "获取账户余额")
    @GetMapping("/getAvailableAmount")
    public Result getAvailableAmount() {
        Long userId = AuthContextHolder.getUserId();
        BigDecimal availableAmount = userAccountService.getAvailableAmount(userId);
        return Result.ok(availableAmount);
    }

    /**
     * 检查及扣减账户余额
     *
     * @param accountDeductVo
     * @return
     */
    @Operation(summary = "检查及扣减账户余额")
    @PostMapping("/checkAndDeduct")
    public Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo) {
        Boolean res = userAccountService.checkAndDeduct(accountDeductVo);
        return res ? Result.ok() : Result.fail();
    }

}

